﻿using B.S.DataBase.MyProject.Domain.RBAC;
using B.S.DataBase.MyProject.ErrorCode;
using B.S.DataBase.MyProject.Infrastructure.Interface;
using B.S.DataBase.MyRead.API.Appicant.Command.Menus;
using MediatR;

namespace B.S.DataBase.MyRead.API.Appicant.CommandHandler.Menus
{
    public class GetMenuAllCommandHandler : IRequestHandler<GetMenuAllCommand, APIResult<List<Menu>>>
    {
        private readonly IBaseRepository<Menu> menuRep;

        public GetMenuAllCommandHandler(IBaseRepository<Menu> menuRep)
        {
            this.menuRep = menuRep;
        }
        public Task<APIResult<List<Menu>>> Handle(GetMenuAllCommand request, CancellationToken cancellationToken)
        {
            APIResult<List<Menu>> result = new APIResult<List<Menu>>();
            var list = menuRep.GetAll().Result;
            if (!string.IsNullOrEmpty(request.MenuName))
            {
                list = list.Where(x => x.MenuName.Contains(request.MenuName));
            }
            list = list.OrderBy(x => x.PId);
            result.Code = APIEnum.成功;
            result.Msg = APIEnum.成功.ToString();
            result.data = list.ToList();
            return Task.FromResult(result);
        }
    }
}
